﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class SysCode : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkSysCreated;
        protected HtmlForm F1081;
        protected GridView gvSysCodeDetail;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnSeqNo;
        protected HtmlInputHidden hdnSortDirection;
        protected HtmlInputHidden hdnSortField;
        protected HtmlInputHidden hdnTotalPages;
        protected HtmlHead Head1;
        protected Label lblDescription;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblSysCodeType;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDescription;
        protected TextBox txtSysCodeType;
        protected UpdatePanel up1;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtSysCodeType.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1800_006");
            }
            else
            {
                if (string.IsNullOrEmpty(base.Request.QueryString["ID"]) || (base.DataEntityViewState == null))
                {
                    try
                    {
                        this.InsertSysCodeInfo();
                    }
                    catch (Exception ex)
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F1800_011") + ex.Message;
                    }
                }
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >  AddSysCodeDetail(); </script>", false);
            }
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtSysCodeType.Text = "";
            this.txtSysCodeType.Enabled = true;
            this.chkSysCreated.Checked = false;
            base.DataEntityViewState = null;
            this.hdnFlag.Value = "0";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.chkSysCreated.Checked)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1800_005");
            }
            else if (!string.IsNullOrEmpty(this.txtSysCodeType.Text.Trim()))
            {
                try
                {
                    List<string> SysCode = new List<string> {
                        this.txtSysCodeType.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(SysCode, "SYS_CODE") + base.GetGlobalMsgResource("F1800_002");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1800_003") + ex.Message;
                }
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtSysCodeType.Text.Trim()) && (this.hdnFlag.Value == "1"))
            {
                try
                {
                    List<string> SysCode = base.GetSelectedIDsInGridView(this.gvSysCodeDetail, "SysCode");
                    List<string> SysCodeType = base.GetSelectedIDsInGridView(this.gvSysCodeDetail, "SysCodeType");
                    if ((SysCode == null) || (SysCode.Count == 0))
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F1800_001");
                        return;
                    }
                    this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(SysCodeType, SysCode, new List<string>(), "SYS_CODE_DETAIL") + base.GetGlobalMsgResource("F1800_002");
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F1800_003") + ex.Message;
                }
                this.SetSysCodeInfo3Form();
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
            }
            else
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1800_004");
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("SysCodeQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("SysCodeList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtSysCodeType.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1800_006");
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateSysCodeInfo();
            }
            else
            {
                this.InsertSysCodeInfo();
            }
        }

        protected void gvSysCodeDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F1081", this.gvSysCodeDetail);
        }

        protected void gvSysCodeDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as SysCodeInfo).SysCodeDetailInfo;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvSysCodeDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvSysCodeDetail.PageIndex * this.gvSysCodeDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        protected void gvSysCodeDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                SysCodeInfo SysCodeInfo = base.DataEntityViewState as SysCodeInfo;
                DataView dv = CADataConverter.ToDataTable(SysCodeInfo.SysCodeDetailInfo).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvSysCodeDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void InsertSysCodeInfo()
        {
            try
            {
                SysCodeInfo sysCodeInfo = new SysCodeInfo();
                if (base.DataEntityViewState != null)
                {
                    sysCodeInfo = base.DataEntityViewState as SysCodeInfo;
                    if ((sysCodeInfo.SysCodeDetailInfo != null) && (sysCodeInfo.SysCodeDetailInfo.Count > 0))
                    {
                        for (int i = 0; i < sysCodeInfo.SysCodeDetailInfo.Count; i++)
                        {
                            sysCodeInfo.SysCodeDetailInfo[i].SysCodeType = this.txtSysCodeType.Text.Trim();
                        }
                    }
                }
                sysCodeInfo.SysCodeType = this.txtSysCodeType.Text;
                sysCodeInfo.Description = this.txtDescription.Text.Trim();
                sysCodeInfo.SystemCreated = this.chkSysCreated.Checked ? "Y" : "N";
                sysCodeInfo.CreatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(sysCodeInfo, "SYS_CODE") + base.GetGlobalMsgResource("F1800_007");
                base.DataEntityViewState = sysCodeInfo;
                this.hdnFlag.Value = "1";
                base.PageQueryEntitySession = sysCodeInfo;
                this.txtSysCodeType.Enabled = false;
                if ((sysCodeInfo.SysCodeDetailInfo != null) && (sysCodeInfo.SysCodeDetailInfo.Count > 0))
                {
                    this.hdnSeqNo.Value = (sysCodeInfo.SysCodeDetailInfo[sysCodeInfo.SysCodeDetailInfo.Count - 1].SeqNo + 10).ToString();
                }
                else
                {
                    this.hdnSeqNo.Value = "10";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1800_008") + ex.Message;
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetSysCodeInfo2Form();
                this.hdnFlag.Value = "1";
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"];
                if (base.Request.QueryString["from"] == "detail")
                {
                    this.SetSysCodeInfo3Form();
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetControlsGvSysCodeDetailData(SysCodeInfo SysCode)
        {
            if (SysCode.SysCodeDetailInfo.Count > 0)
            {
                this.gvSysCodeDetail.DataSource = SysCode.SysCodeDetailInfo;
                this.gvSysCodeDetail.DataBind();
            }
        }

        private void SetControlsSysCodeText(SysCodeInfo SysCode)
        {
            if (SysCode != null)
            {
                this.txtSysCodeType.Text = SysCode.SysCodeType;
                this.txtSysCodeType.Enabled = false;
                this.txtDescription.Text = SysCode.Description;
                if (SysCode.SystemCreated.ToUpper() == "Y")
                {
                    this.chkSysCreated.Checked = true;
                }
                else
                {
                    this.chkSysCreated.Checked = false;
                }
            }
        }

        private void SetSysCodeInfo2Form()
        {
            try
            {
                string SysCodeID = base.Request.QueryString["ID"].ToString();
                SysCodeInfo SysCode = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(SysCodeID, "SYS_CODE") as SysCodeInfo;
                base.DataEntityViewState = SysCode;
                this.SetControlsSysCodeText(SysCode);
                this.SetControlsGvSysCodeDetailData(SysCode);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void SetSysCodeInfo3Form()
        {
            string SysCodeID = string.Empty;
            if (!string.IsNullOrEmpty(this.txtSysCodeType.Text))
            {
                SysCodeID = this.txtSysCodeType.Text;
            }
            else
            {
                SysCodeID = base.Request.QueryString["ID"];
            }
            SysCodeInfo SysCode = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(SysCodeID, "SYS_CODE") as SysCodeInfo;
            base.DataEntityViewState = SysCode;
            base.PageQueryEntitySession = SysCode;
            this.SetControlsSysCodeText(SysCode);
            this.SetControlsGvSysCodeDetailData(SysCode);
        }

        private void UpdateSysCodeInfo()
        {
            try
            {
                SysCodeInfo sysCodeInfo = base.DataEntityViewState as SysCodeInfo;
                sysCodeInfo.Description = this.txtDescription.Text.Trim();
                sysCodeInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(sysCodeInfo, "SYS_CODE") + base.GetGlobalMsgResource("F1800_009");
                base.PageQueryEntitySession = sysCodeInfo;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F1800_010") + ex.Message;
            }
        }
    }
}

